package com.wsm;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.StringUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 * @author wangshmin
 *
 */
@Slf4j
@EnableTransactionManagement
@MapperScan("com.wsm.*.mapper")
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableCaching
@EnableAsync
public class SpringbootDemoApplication {
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(SpringbootDemoApplication.class);
        springApplication.setBannerMode(Banner.Mode.CONSOLE);
        ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args);
        ConfigurableEnvironment environment = configurableApplicationContext.getEnvironment();
        getMessage(environment);
    }

    private static void getMessage(ConfigurableEnvironment env) {
        String name = env.getProperty("spring.application.name");
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        String address;
        try {
            InetAddress candidateAddress = null;
            for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
                NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
                // 在所有的接口下再遍历IP
                for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
                    InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
                    if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
                        if (inetAddr.isSiteLocalAddress()) {
                            // 如果是site-local地址，就是它了
                            candidateAddress = inetAddr;
                            break;
                        } else if (candidateAddress == null) {
                            // site-local类型的地址未被发现，先记录候选地址
                            candidateAddress = inetAddr;
                        }
                    }
                }
            }
            if (candidateAddress != null) {
                address = candidateAddress.getHostAddress();
            } else {
                address = InetAddress.getLocalHost().getHostAddress();
            }
        } catch (UnknownHostException | SocketException e) {
            address = "127.0.0.1";
        }
        name = StringUtils.isEmpty(name) ? "" : name;
        port = StringUtils.isEmpty(port) ? "8080" : port;
        path = StringUtils.isEmpty(path) ? "" : path;
        log.info("\n" +
                        "》》》》》》》》》》》》》》》》》》》》》》\n" +
                        "{}启动成功\t\t\t\t\n" +
                        "登陆地址\t\t\t  http://{}\t\n" +
                        "》》》》》》》》》》》》》》》》》》》》》》\n"
                , name,
                address + ":" + port + path);
    }
}
